﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml;
using System.Xml.Serialization;


namespace SusyGame_Model
{
    [XmlInclude(typeof(ArrowDefender))]
    [Serializable]
    public class Defender : Sprite
    {
        //Atributos
        
        protected int price;
        protected int damage;
        protected float radius;
        protected Enemy target;
        [NonSerialized]
        private Texture2D weaponTexture;
        
        private float weaponTimer;
        private List<Weapon> weaponList = new List<Weapon>();
        private int tipo;

        public int Tipo
        {
            get { return tipo; }
            set { tipo = value; }
        }

        protected Texture2D WeaponTexture

        {
            get { return weaponTexture; }
            set { weaponTexture = value; }
        }
       
        protected float WeaponTimer
        {
            get { return weaponTimer; }
            set { weaponTimer = value; }
        }
        
        protected List<Weapon> WeaponList
        {
            get { return weaponList; }
            set { weaponList = value; }
        }

        //Propiedades
        public Enemy Target
        {
            get { return target; }
            set { target = value; }
        }

        public float Radius
        {
            get { return radius; }
            set { radius = value; }
        }

        public int Damage
        {
            get { return damage; }
            set { damage = value; }
        }
        
        public int Price
        {
            get { return price; }
            set { price = value; }
        }

        public virtual bool HasTarget
        {
            // Veo si la torre tiene algun enemigo (para el spike defender)
            get { return target != null; }
        }

        //Métodos
        public Defender()
        {
        }

        public Defender(Texture2D texture, Texture2D weaponTexture, Vector2 position): base(texture, position)
        {
            this.weaponTexture = weaponTexture;
        }

        public virtual bool IsInRange(Vector2 position)
        {
            if ((Vector2.Distance(base.Center, position) <= radius) && (base.Center.X < position.X))
                return true;

            return false;
        }

        //METODO QUE VERIFICA SI ME DEFENSOR HA SIDO PASADO POR EL ENEMIGO
        public bool HaveCrashed()
        {
            if (target != null)
            {
                if (!IsInRange(target.Center) && !target.IsDead)
                {
                    return true;
                }
            }
            return false;
        }

        public virtual void GetClosestEnemy(List<Enemy> enemies)
        {
            target = null;
            float smallestRange = radius;

            foreach (Enemy enemy in enemies)
            {
                if ((base.Position.Y == enemy.Position.Y) &&(Vector2.Distance(base.Center, enemy.Center) < smallestRange))
                {
                    smallestRange = Vector2.Distance(center, enemy.Center);
                    target = enemy;
                }
            }
        }

        protected void FaceTarget()
        {
            Vector2 direction = center - target.Center;
            direction.Normalize();

            rotation = (float)Math.Atan2(-direction.X, direction.Y);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            WeaponTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (target != null)
            {
                FaceTarget();

                if (!IsInRange(target.Center) || target.IsDead)
                {
                    target = null;
                    WeaponTimer = 0;
                    base.Rotation = 0;
                }
            }              
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            foreach (Weapon weapon in WeaponList)
                weapon.Draw(spriteBatch);

            base.Draw(spriteBatch);
        }
    }
}
